<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>json_perf.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_json_json_perf.js.html">json_perf.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2012 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview JSON performance tests.
<a name="line17"></a> */
<a name="line18"></a>
<a name="line19"></a>goog.provide(&#39;goog.jsonPerf&#39;);
<a name="line20"></a>
<a name="line21"></a>goog.require(&#39;goog.dom&#39;);
<a name="line22"></a>goog.require(&#39;goog.json&#39;);
<a name="line23"></a>goog.require(&#39;goog.math&#39;);
<a name="line24"></a>goog.require(&#39;goog.string&#39;);
<a name="line25"></a>goog.require(&#39;goog.testing.PerformanceTable&#39;);
<a name="line26"></a>goog.require(&#39;goog.testing.PropertyReplacer&#39;);
<a name="line27"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line28"></a>
<a name="line29"></a>goog.setTestOnly(&#39;goog.jsonPerf&#39;);
<a name="line30"></a>
<a name="line31"></a>var table = new goog.testing.PerformanceTable(
<a name="line32"></a>    goog.dom.getElement(&#39;perfTable&#39;));
<a name="line33"></a>
<a name="line34"></a>var stubs = new goog.testing.PropertyReplacer();
<a name="line35"></a>
<a name="line36"></a>function tearDown() {
<a name="line37"></a>  stubs.reset();
<a name="line38"></a>}
<a name="line39"></a>
<a name="line40"></a>function testSerialize() {
<a name="line41"></a>  var obj = populateObject({}, 50, 4);
<a name="line42"></a>
<a name="line43"></a>  table.run(function() {
<a name="line44"></a>    var s = JSON.stringify(obj);
<a name="line45"></a>  }, &#39;Stringify using JSON.stringify&#39;);
<a name="line46"></a>
<a name="line47"></a>  table.run(function() {
<a name="line48"></a>    var s = goog.json.serialize(obj);
<a name="line49"></a>  }, &#39;Stringify using goog.json.serialize&#39;);
<a name="line50"></a>}
<a name="line51"></a>
<a name="line52"></a>function testParse() {
<a name="line53"></a>  var obj = populateObject({}, 50, 4);
<a name="line54"></a>  var s = JSON.stringify(obj);
<a name="line55"></a>
<a name="line56"></a>  table.run(function() {
<a name="line57"></a>    var o = JSON.parse(s);
<a name="line58"></a>  }, &#39;Parse using JSON.parse&#39;);
<a name="line59"></a>
<a name="line60"></a>  table.run(function() {
<a name="line61"></a>    var o = goog.json.parse(s);
<a name="line62"></a>  }, &#39;Parse using goog.json.parse&#39;);
<a name="line63"></a>
<a name="line64"></a>  table.run(function() {
<a name="line65"></a>    var o = goog.json.unsafeParse(s);
<a name="line66"></a>  }, &#39;Parse using goog.json.unsafeParse&#39;);
<a name="line67"></a>}
<a name="line68"></a>
<a name="line69"></a>
<a name="line70"></a>/**
<a name="line71"></a> * @param {!Object} obj The object to add properties to.
<a name="line72"></a> * @param {number} numProperties The number of properties to add.
<a name="line73"></a> * @param {number} depth The depth at which to recursively add properties.
<a name="line74"></a> * @return {!Object} The object given in obj (for convenience).
<a name="line75"></a> */
<a name="line76"></a>function populateObject(obj, numProperties, depth) {
<a name="line77"></a>  if (depth == 0) {
<a name="line78"></a>    return randomLiteral();
<a name="line79"></a>  }
<a name="line80"></a>
<a name="line81"></a>  // Make an object with a mix of strings, numbers, arrays, objects, booleans
<a name="line82"></a>  // nulls as children.
<a name="line83"></a>  for (var i = 0; i &lt; numProperties; i++) {
<a name="line84"></a>    var bucket = goog.math.randomInt(3);
<a name="line85"></a>    switch (bucket) {
<a name="line86"></a>      case 0:
<a name="line87"></a>        obj[i] = randomLiteral();
<a name="line88"></a>        break;
<a name="line89"></a>      case 1:
<a name="line90"></a>        obj[i] = populateObject({}, numProperties, depth - 1);
<a name="line91"></a>        break;
<a name="line92"></a>      case 2:
<a name="line93"></a>        obj[i] = populateObject([], numProperties, depth - 1);
<a name="line94"></a>        break;
<a name="line95"></a>    }
<a name="line96"></a>  }
<a name="line97"></a>}
<a name="line98"></a>
<a name="line99"></a>
<a name="line100"></a>function randomLiteral() {
<a name="line101"></a>  var bucket = goog.math.randomInt(3);
<a name="line102"></a>  switch (bucket) {
<a name="line103"></a>    case 0:
<a name="line104"></a>      return goog.string.getRandomString();
<a name="line105"></a>    case 1:
<a name="line106"></a>      return Math.random();
<a name="line107"></a>    case 2:
<a name="line108"></a>      return Math.random() &gt;= .5;
<a name="line109"></a>  }
<a name="line110"></a>  return null;
<a name="line111"></a>}
</pre>


</body>
</html>
